<!-- 采用封装方法 -->
<template>
  <div class="info-box">
    <el-button type="primary" class="add-stu" @click="addStudent"
      >新增</el-button
    >
    <!-- 表格区域 -->
    <el-table :data="comData" v-loading="loading" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="father" label="父亲" align="center">
      </el-table-column>
      <el-table-column prop="mother" label="母亲" align="center">
      </el-table-column>
      <el-table-column prop="time" label="入学时间" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="pagination"
    >
    </el-pagination>
    <!-- 弹窗区域 -->
    <el-dialog
      :title="state ? '添加学生信息' : '修改学生信息'"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="父亲姓名"
          :label-width="formLabelWidth"
          prop="father"
        >
          <el-input v-model="form.father" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="母亲姓名"
          :label-width="formLabelWidth"
          prop="mother"
        >
          <el-input v-model="form.mother" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="入学时间"
          :label-width="formLabelWidth"
          prop="time"
        >
          <el-date-picker
            v-model="form.time"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="家庭地址"
          :label-width="formLabelWidth"
          prop="address"
        >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
        <el-button @click="closeInfo('form')">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getData, changeInfo, delData } from '@/utils/table'

export default {
  data () {
    return {
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '100px',
      state: true,
      currentPage: 1,
      loading: true,
      pageSize: 10,
      total: 0,
      form: {
        name: '',
        sex: '1',
        age: '',
        father: '',
        mother: '',
        time: '',
        address: '',
        phone: '',
      },
      rules: {
        name: [{ required: true, message: '请输入姓名' }],
        sex: [{ required: true }],
        age: [{ required: true, message: '请输入年龄' }],
        time: [{ required: true, message: '请输入地址' }],
        address: [{ required: true, message: '请输入入学时间' }],
        phone: [{ required: true, message: '请输入联系方式' }],
      }
    }
  },
  computed: {
    comData () {
      return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    }
  },
  methods: {
    edit (row) {
      console.log(row)
      this.$nextTick(() => {
        this.form = { ...row }
      })
      this.state = false
      this.dialogFormVisible = true
    },
    del (row) {
      delData(this, '/info', row.id, getData)
    },
    // 分页操作方法
    handleSizeChange (val) {
      this.pageSize = val
      this.currentPage = 1
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
    },
    // 添加学生信息方法
    addStudent () {
      this.state = true
      this.form = {
        name: '',
        sex: '1',
        age: '',
        father: '',
        mother: '',
        time: '',
        address: '',
        phone: '',
      }
      this.dialogFormVisible = true
    },
    // 取消操作
    closeInfo (form) {
      this.$refs[form].resetFields()
      this.dialogFormVisible = false
    },
    // 提交表单
    submitForm (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          let method = this.state ? 'post' : 'put'
          changeInfo(this, method, '/info', this.form, getData)
        }
      })
    }
  },
  created () {
    getData(this, '/info', {}, ['sex'])
  }
}
</script>

<style lang="less">
.info-box {
  .add-stu {
    margin-bottom: 5px;
  }
  .el-form {
    .el-form-item {
      margin-bottom: 20px;
    }
  }
  .pagination {
    margin-top: 10px;
  }
}
</style>